package com.example.demo.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.generator.entiy.TAddress;
import com.example.demo.generator.entiy.TCurrency;
import com.example.demo.generator.service.TAddressService;
import com.example.demo.generator.service.TCurrencyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "钱包地址接口")
@CrossOrigin("*")
public class Address {

    @Autowired
    public TAddressService tAddressService;
    @Autowired
    public TCurrencyService tCurrencyService;
    //获取钱包地址列表
    @GetMapping("getaddresslist")
    @ApiOperation("获取钱包地址列表")
    public R getaddresslist(@RequestParam(value = "pageNum", defaultValue = "") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "") Integer pageSize, @RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "id", defaultValue = "") Integer id) {

        QueryWrapper<TAddress>tAddressQueryWrapper=new QueryWrapper<>();
        if(pageNum!=null&&pageSize!=null){
            if(name!=null){
                tAddressQueryWrapper.like("AccountID",name);
            }
            if(id!=null){
                tAddressQueryWrapper.eq("CurrencyID",id);
            }
            PageHelper.startPage(pageNum,pageSize);
            List<TAddress>tAddressList=tAddressService.list(tAddressQueryWrapper);
            for(int i=0;i<tAddressList.size();i++){
                tAddressList.get(i).setCurrencyname(getcurrency(tAddressList.get(i).getCurrencyid()).getName());
                tAddressList.get(i).setCurrencyalaisname(getcurrency(tAddressList.get(i).getCurrencyid()).getAliasname());
            }
            PageInfo<TAddress>pageInfo=new PageInfo<>(tAddressList);
            return R.ok(pageInfo);
        }else{
            if(name!=null){
                tAddressQueryWrapper.like("AccountID",name);
            }
            if(id!=null){
                tAddressQueryWrapper.eq("CurrencyID",id);
            }
            List<TAddress>tAddressList=tAddressService.list(tAddressQueryWrapper);
            for(int i=0;i<tAddressList.size();i++){
                tAddressList.get(i).setCurrencyname(getcurrency(tAddressList.get(i).getCurrencyid()).getName());
                tAddressList.get(i).setCurrencyalaisname(getcurrency(tAddressList.get(i).getCurrencyid()).getAliasname());
            }
            return R.ok(tAddressList);
        }
    }

    //获取币种名称和别名
    public TCurrency getcurrency(Integer id){
        TCurrency tCurrency=tCurrencyService.getById(id);
        return tCurrency;
    }


}
